import 'base_record_dialog.dart';
import 'package:flutter/material.dart';
import '../service/database_service.dart';
import '../model/health_record.dart';

class AddFeedingRecordDialog extends BaseRecordDialog {
  const AddFeedingRecordDialog({Key? key, required String babyId})
    : super(key: key, babyId: babyId);

  @override
  _AddFeedingRecordDialogState createState() => _AddFeedingRecordDialogState();
}

class _AddFeedingRecordDialogState
    extends BaseRecordDialogState<AddFeedingRecordDialog> {
  double amount = 0;

  @override
  String getDialogTitle() => '记录喂奶';

  @override
  Widget buildCustomContent(BuildContext context) {
    return TextField(
      decoration: InputDecoration(
        labelText: '喂奶量',
        suffixText: 'ml',
        border: OutlineInputBorder(),
      ),
      keyboardType: TextInputType.number,
      onChanged: (value) => amount = double.tryParse(value) ?? 0,
    );
  }

  @override
  Future<void> saveRecord(BuildContext context) async {
    if (amount <= 0) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('请输入有效的喂奶量')));
      return;
    }

    try {
      await DatabaseService().addHealthRecord(
        HealthRecord(
          babyId: widget.babyId, // Now using the correct String type
          type: RecordType.feeding,
          timestamp: selectedDateTime,
          data: {'amount': amount, 'notes': notes},
        ),
      );
      Navigator.pop(context);
    } catch (e) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('保存失败: $e')));
    }
  }
}
